<?php
class SoftwareVersion
{
    private $property = array();
    private $valid = false;
    private $valid_properties = "SoftwareVersionID,SoftwareID,VersionNumber,UpdateDate,Active";

    public static function createFromArray($values)
    {
        if (!is_array($values)) return null;
        if (count($values) <= 0) return null;

        $S = new SoftwareVersion();
        $S->FromArray($values);
        return $S;
    }

    public static function createFromCommand(command $cmd)
    {
        logger::called();

        if (!is_array($cmd->Data())) return null;
        if (count($cmd->Data()) <= 0) return null;

        $S = new SoftwareVersion();
        $S->FromArray($cmd->Data());
        return $S;
    }
    
    public function __construct(){}

    public function SoftwareVersionID()
    {
        return $this->GetSetData("SoftwareVersionID",null);
    }
    
    public function SoftwareID()
    {
        return $this->GetSetData("SoftwareID",null);
    }
    
    public function VersionNumber()
    {
        return $this->GetSetData("VersionNumber",null);
    }
    
    public function UpdateDate()
    {
        return $this->GetSetData("UpdateDate",null);
    }
    
    public function Active()
    {
        return $this->GetSetData("Active",null);
    }

    public function StorableData()
    {
        $result["SoftwareVersionID"] = $this->SoftwareVersionID();
        $result["SoftwareID"] = $this->SoftwareID();
        $result["VersionNumber"] = $this->VersionNumber();
        $result["UpdateDate"] = $this->UpdateDate();
        $result["Active"] = $this->Active();

        return $result;
    }

    public function UploadFile($postedFile)
    {
        $path_parts = pathinfo($postedFile["name"]);
        $ext = $path_parts['extension'];
        $newFilename = $postedFile["name"];
        
        //$newFilename = $this->SoftwareVersionID() . "-" . $this->SoftwareID() . "-". $this->VersionNumber() . "." . $ext;
        //$newPathName = "../../content/software/" . $newFilename;
        /*
        if (!file::reallyExists(self::SoftwareStoredPath()."/".$this->SoftwareID()))
        {
            file::mkdir_safe(self::SoftwareStoredPath()."/".$this->SoftwareID());
        }
        if (!file::reallyExists(self::SoftwareStoredPath()."/".$this->SoftwareID()."/".$this->VersionNumber()))
        {
            file::mkdir_safe(self::SoftwareStoredPath()."/".$this->SoftwareID()."/".$this->VersionNumber());
        }
        */
        $newPathName = "../../content/software/".$this->SoftwareID()."/".$this->SoftwareVersionID();
        
        echo $newFilename . '<br />';
        echo $newPathName;
        
        @move_uploaded_file($postedFile['tmp_name'], $newPathName);
        if (!file::reallyExists($newPathName)) return FALSE;

        //$updated = ns_dao_software::updateSoftwareVersion($this->SoftwareVersionID(), "FileLocation", "");
        //$updated = ns_dao_software::updateSoftwareVersion($this->SoftwareVersionID(), "FileLocation", $newFilename);
        
        //$this->FileLocation($newFilename);
        return $updated;
    }
    
    public static function AvatarImagePath($fileName) {
        return configuration::$APPLICATION_FOLDER . "/content/softwareAvatar/" . $fileName;
        //return "../../content/softwareAvatar/" . $fileName;
    }
    
    public static function SoftwareStoredPath($fileName = null, $sid = null, $vid = null)
    {
        if (is_null($fileName) && is_null($sid) && is_null($vid))
        {
            return configuration::$APPLICATION_FOLDER."/content/software/";
        }
        else
        {
            return configuration::$APPLICATION_FOLDER."/content/software/".$sid."/".$vid."/".$fileName;
        }
    }

    public function UpdateSoftwareVersionToDatabase()
    {
        $result = ns_dao_software::updateSoftwareVersionByArray($this->property["SoftwareVersionID"], $this->property);
        return $result;
    }

    private function GetSetData($property_name,$value = null)
    {
        //if (!$this->checkPropertyName($property_name)) return null;
        if (is_null($value)) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }

    private function checkPropertyName($property_name)
    {
        foreach (explode(",",$this->valid_properties) as $allowed_prop)
            if ($allowed_prop == $property_name) return true;

        return false;
    }


    /*
     * Populate from an array
     */
    public function FromArray($values)
    {
        if (!is_array($values)) return null;

        foreach ($values as $key => $value)
            $this->GetSetData($key,$value);

    }


    public function __toString()
    {
        return print_r($this->property,true);
    }


}

?>
